package com.expdatacloud.service.impl;

import com.expdatacloud.entity.IqcDetectionDetail;
import com.expdatacloud.mapper.IqcDetectionDetailMapper;
import com.expdatacloud.service.IqcDetectionDetailService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * IQC-检测表 服务实现类
 * </p>
 *
 * @author jie
 * @since 2019-11-19
 */
@Service
@Transactional
public class IqcDetectionDetailServiceImpl implements IqcDetectionDetailService {

    @Autowired
    private IqcDetectionDetailMapper iqcDetectionDetailMapper;

    @Override
    public List<IqcDetectionDetail> list(IqcDetectionDetail iqcDetectionDetail) {
        return iqcDetectionDetailMapper.list(iqcDetectionDetail);
    }

    @Override
    public int update(IqcDetectionDetail iqcDetectionDetail, String user) {
        iqcDetectionDetail.setConformityQuantity(iqcDetectionDetail.getSampleQuantity() - iqcDetectionDetail.getUnqualifiedQuantity());
        iqcDetectionDetail.setEditWho(user);
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        iqcDetectionDetail.setEditDate(dateFormat.format(new Date()));
        iqcDetectionDetail.setReceiveTime(dateFormat.format(new Date()));
        double inventoryQuantity = iqcDetectionDetail.getQuarantineQuantity() - iqcDetectionDetail.getRejectionQuantity() -
                iqcDetectionDetail.getSampleQuantity();
        double checkBackQuantity = iqcDetectionDetail.getRejectionQuantity() + iqcDetectionDetail.getSampleQuantity();
        iqcDetectionDetail.setInventoryQuantity(inventoryQuantity);
        iqcDetectionDetail.setCheckBackQuantity(checkBackQuantity);
        return iqcDetectionDetailMapper.update(iqcDetectionDetail);
    }

    @Override
    public IqcDetectionDetail calculateNumber(@Param("receiptKey") String receiptKey, @Param("sku") String sku) {
        return iqcDetectionDetailMapper.calculateNumber(receiptKey, sku);
    }


    @Override
    public void cleanData(IqcDetectionDetail iqcDetectionDetail, String user, String noOk) {
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        iqcDetectionDetail.setEditWho(user);
        iqcDetectionDetail.setEditDate(dateFormat.format(new Date()));
        if (!"拒收".equals(noOk)) {
            iqcDetectionDetailMapper.cleanData(iqcDetectionDetail);
        } else {
            iqcDetectionDetailMapper.updateAll(iqcDetectionDetail);
        }
    }

}
